package com.cetccloud.ap.common;

import com.cetccloud.ap.config.ProxyConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @ClassName SafeChecker
 * @Description
 * @Author superz
 * @Date 2021/4/25 15:02
 * @Version 1.0
 **/
@Component
public class SafeChecker {

    @Autowired
    private ProxyConfig proxyConfig;

    private AntPathMatcher pathMatcher = new AntPathMatcher();

    public boolean httpCanProxy(String url) {
        List<String> whitelist = proxyConfig.getHttp().getWhitelist();
        if (CollectionUtils.isEmpty(whitelist)) {
            return false;
        }
        return whitelist.stream().anyMatch(pattern -> pathMatcher.match(pattern, url));
    }

}
